package org.hot100_0.leetcode142;

import org.leetcode.easy.ListNode;

public class Solution {
    public ListNode detectCycle(ListNode head) {

        if (head==null || head.next==null){
            return head;
        }

        ListNode slow = head;
        ListNode fast = head.next;


        while (fast!=null){
            if (slow==fast){
                return slow;
            }

            slow = slow.next;
            fast = fast.next;
            if (fast!=null){
                fast = fast.next;
            }
        }

        return null;
    }
}
